/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.unix;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.FalseExit;
import fr.x9c.cadmium.kernel.Fatal;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides implementation for some execution-related primitives.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Execvp {

    /**
     * No instance of this class.
     */
    private Execvp() {
    } // end empty constructor

    /**
     * Simulates <i>execvp</i> by executing given a program, then stopping
     * program.
     * @param ctxt context
     * @param path program to execute
     * @param args program arguments
     * @return <i>unit</i>
     * @throws Fail.Exception if program cannot be executed
     */
    @Primitive
    public static Value unix_execvp(final CodeRunner ctxt,
                                    final Value path,
                                    final Value args)
        throws Fail.Exception, Fatal.Exception, FalseExit {
        return Execv.unix_execv(ctxt, lookupPath(path), args);
    } // end method 'unix_execvp(CodeRunner, Value, Value)'

    /**
     * Simulates <i>execvpe</i> by executing given a program, then stopping
     * program.
     * @param ctxt context
     * @param path program to execute
     * @param args program arguments
     * @param env program environment
     * @return <i>unit</i>
     * @throws Fail.Exception if program cannot be executed
     */
    @Primitive
    public static Value unix_execvpe(final CodeRunner ctxt,
                                     final Value path,
                                     final Value args,
                                     final Value env)
        throws Fail.Exception, Fatal.Exception, FalseExit {
        return Execve.unix_execve(ctxt, lookupPath(path), args, env);
    } // end method 'unix_execvpe(CodeRunner, Value, Value, Value)'

    /**
     * Converts a path according to environment.
     * @param path file path
     * @return path, as enriched by environment
     */
    private static Value lookupPath(final Value path) {
        return Value.createFromBlock(Block.createString(Unix.lookupPath(path.asBlock().asString())));
    } // end method 'lookupPath(Value)'

} // end class 'Execvp'
